package com.lakeworks.service.impl;

import com.lakeworks.common.enums.PostRecordEnum;
import com.lakeworks.common.enums.StatusRecordEnum;
import com.lakeworks.domain.status.PostRecord;
import com.lakeworks.domain.status.StatusRecord;
import com.lakeworks.service.IPostWrapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.lakeworks.common.utils.SecurityUtils.getUserId;
import static com.lakeworks.common.utils.SecurityUtils.getUserName;

/**
 * @Author: Auxps
 * @Date: 2023/2/24
 * Description:
 * 操作记录工具类
 */
@Component
public class OperateRecordUtil {

    private static IPostWrapService postWrapService;

    @Autowired
    public OperateRecordUtil(IPostWrapService postWrapService) {
        this.postWrapService = postWrapService;
    }

    /**
     * 保存产品操作记录（核查、质检、包装、拣货、打包）
     *
     * @param postRecordEnum 操作枚举
     * @param oprProductId   作业产品id
     * @param flag           是否工厂 作业人员 操作  true:人工     false:robot
     */
    public static void savePostRecord(PostRecordEnum postRecordEnum, Long oprProductId, boolean flag) {
        String userName = flag ? getUserName() : getUserName().concat("_robot");
        postWrapService.savePostRecord(postRecordEnum.getTable(), oprProductId, getUserId(), userName);
    }

    public static void commonPostRecord(PostRecordEnum postRecordEnum, Long oprProductId) {
        postWrapService.commonPostRecord(postRecordEnum.getTable(), oprProductId);
    }

    /**
     * 保存操作状态（入厂单、作业产品、出厂单）
     *
     * @param statusRecordEnum 操作对象枚举
     * @param objId            入厂单、产品、出厂单id
     * @param status           状态
     * @param flag             是否工厂 作业人员 操作  true:人工     false:robot
     */
    public static void saveStatusRecord(StatusRecordEnum statusRecordEnum, Long objId, String status, boolean flag) {
        String userName = flag ? getUserName() : getUserName().concat("_robot");
        postWrapService.saveStatusRecord(statusRecordEnum.getTable(), statusRecordEnum.getField(), objId, status, getUserId(), userName);
    }

    /**
     * 入厂单导入、出厂单导入、推送出厂物流 时，使用此方法
     */
    public static void saveImportRecord(StatusRecordEnum statusRecordEnum, Long objId, String status) {
        postWrapService.saveStatusRecord(statusRecordEnum.getTable(), statusRecordEnum.getField(), objId, status);
    }

    /**
     * 渠道、操作员
     */
    public static void commonImportRecord(StatusRecordEnum statusRecordEnum, Long objId, String status) {
        postWrapService.commonStatusRecord(statusRecordEnum.getTable(), statusRecordEnum.getField(), objId, status);
    }

    /**
     * 根据产品id查询操作记录
     */
    public static PostRecord getPostRecord(PostRecordEnum postRecordEnum, Long oprProductId) {
        return postWrapService.getPostRecord(postRecordEnum.getTable(), oprProductId);
    }


    /**
     * 根据对应id查询对应状态记录
     *
     * @param objId  入厂单id、产品id、出厂单id
     * @param status 状态
     */
    public static StatusRecord getStatusRecord(StatusRecordEnum statusRecordEnum, Long objId, String status) {
        return postWrapService.getStatusRecord(statusRecordEnum.getTable(), statusRecordEnum.getField(), objId, status);
    }
}